/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.client.ZFilterAction;
/*     */ import com.zimbra.client.ZFilterAction.ZFileIntoAction;
/*     */ import com.zimbra.client.ZFilterCondition;
/*     */ import com.zimbra.client.ZFilterCondition.HeaderOp;
/*     */ import com.zimbra.client.ZFilterCondition.ZHeaderCondition;
/*     */ import com.zimbra.client.ZFilterRule;
/*     */ import com.zimbra.client.ZFilterRules;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.service.util.SpamHandler;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.StringReader;
/*     */ import java.util.Arrays;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSpam
/*     */   extends TestCase
/*     */ {
/*  56 */   private static final String NAME_PREFIX = TestSpam.class.getSimpleName();
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String SPAM_NAME = "user2";
/*     */   private static final String HAM_NAME = "user3";
/*     */   private static final String REMOTE_USER_NAME = "user4";
/*     */   private String mOriginalSpamHeaderValue;
/*     */   private String mOriginalSpamAccount;
/*     */   private String mOriginalHamAccount;
/*     */   private String mOriginalSieveScript;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  69 */     cleanUp();
/*     */     
/*  71 */     Config config = Provisioning.getInstance().getConfig();
/*  72 */     this.mOriginalSpamHeaderValue = config.getSpamHeaderValue();
/*  73 */     this.mOriginalSpamAccount = config.getSpamIsSpamAccount();
/*  74 */     this.mOriginalHamAccount = config.getSpamIsNotSpamAccount();
/*     */     
/*  76 */     Account account = TestUtil.getAccount("user1");
/*  77 */     this.mOriginalSieveScript = account.getMailSieveScript();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void xtestSpam()
/*     */     throws Exception
/*     */   {
/*  85 */     String coreContent = TestUtil.getTestMessage(NAME_PREFIX + " testSpam", "user1", "user1", null);
/*  86 */     MimeMessage msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(coreContent.getBytes()));
/*  87 */     assertFalse(SpamHandler.isSpam(msg));
/*     */     
/*     */ 
/*  90 */     String headerName = Provisioning.getInstance().getConfig().getSpamHeader();
/*  91 */     String singleLineSpamContent = headerName + ": YES\r\n" + coreContent;
/*  92 */     msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(singleLineSpamContent.getBytes()));
/*  93 */     assertTrue(SpamHandler.isSpam(msg));
/*     */     
/*     */ 
/*  96 */     Provisioning.getInstance().getConfig().setSpamHeaderValue("spam.*");
/*  97 */     String folderSpamContent = headerName + ": spam, SpamAssassin (score=5.701, required 5,\r\n" + "   DCC_CHECK 1.37, FH_RELAY_NODNS 1.45, RATWARE_RCVD_PF 2.88)\r\n" + coreContent;
/*     */     
/*  99 */     msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(folderSpamContent.getBytes()));
/* 100 */     assertTrue(SpamHandler.isSpam(msg));
/*     */   }
/*     */   
/*     */   public void testSpamHandler()
/*     */     throws Exception
/*     */   {
/* 106 */     List<String> zimbraServiceInstalled = Arrays.asList(Provisioning.getInstance().getLocalServer().getServiceInstalled());
/* 107 */     if ((zimbraServiceInstalled == null) || (zimbraServiceInstalled.isEmpty()) || (!zimbraServiceInstalled.contains("antispam"))) {
/* 108 */       return;
/*     */     }
/* 110 */     Config config = Provisioning.getInstance().getConfig();
/* 111 */     config.setSpamIsSpamAccount(TestUtil.getAddress("user2"));
/* 112 */     config.setSpamIsNotSpamAccount(TestUtil.getAddress("user3"));
/*     */     
/*     */ 
/* 115 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 116 */     ZFilterCondition cond = new ZFilterCondition.ZHeaderCondition("Subject", ZFilterCondition.HeaderOp.CONTAINS, NAME_PREFIX);
/* 117 */     ZFolder spamFolder = mbox.getFolderById(Integer.toString(4));
/* 118 */     ZFolder inboxFolder = mbox.getFolderById(Integer.toString(2));
/* 119 */     ZFilterAction action = new ZFilterAction.ZFileIntoAction(spamFolder.getPath());
/* 120 */     ZFilterRule rule = new ZFilterRule(NAME_PREFIX + " testSpamHandler", true, true, Arrays.asList(new ZFilterCondition[] { cond }), Arrays.asList(new ZFilterAction[] { action }));
/* 121 */     ZFilterRules rules = new ZFilterRules(Arrays.asList(new ZFilterRule[] { rule }));
/* 122 */     mbox.saveIncomingFilterRules(rules);
/*     */     
/*     */ 
/* 125 */     String subject = NAME_PREFIX + " testSpamHandler";
/* 126 */     TestUtil.addMessageLmtp(subject, "user1", "user1");
/* 127 */     ZMessage msg = TestUtil.getMessage(mbox, "in:" + spamFolder.getPath() + " subject:\"" + subject + "\"");
/* 128 */     ZMailbox spamMbox = TestUtil.getZMailbox("user2");
/* 129 */     ZMessage reportMsg = TestUtil.waitForMessage(spamMbox, "zimbra-spam-report spam");
/* 130 */     validateSpamReport(TestUtil.getContent(spamMbox, reportMsg.getId()), TestUtil.getAddress("user1"), "spam", "filter", null, spamFolder.getPath(), null);
/*     */     
/* 132 */     spamMbox.deleteMessage(reportMsg.getId());
/*     */     
/*     */ 
/* 135 */     mbox.moveMessage(msg.getId(), Integer.toString(2));
/* 136 */     ZMailbox hamMbox = TestUtil.getZMailbox("user3");
/* 137 */     reportMsg = TestUtil.waitForMessage(hamMbox, "zimbra-spam-report ham");
/* 138 */     validateSpamReport(TestUtil.getContent(hamMbox, reportMsg.getId()), TestUtil.getAddress("user1"), "ham", "move", spamFolder.getPath(), inboxFolder.getPath(), null);
/*     */     
/* 140 */     hamMbox.deleteMessage(reportMsg.getId());
/*     */     
/*     */ 
/* 143 */     mbox.moveMessage(msg.getId(), Integer.toString(4));
/* 144 */     reportMsg = TestUtil.waitForMessage(spamMbox, "zimbra-spam-report spam");
/* 145 */     validateSpamReport(TestUtil.getContent(spamMbox, reportMsg.getId()), TestUtil.getAddress("user1"), "spam", "move", inboxFolder.getPath(), spamFolder.getPath(), null);
/*     */     
/* 147 */     spamMbox.deleteMessage(reportMsg.getId());
/*     */     
/*     */ 
/* 150 */     ZMailbox remoteMbox = TestUtil.getZMailbox("user4");
/* 151 */     String mountpointPath = NAME_PREFIX + " remote";
/* 152 */     TestUtil.createMountpoint(remoteMbox, "/Inbox", mbox, mountpointPath);
/* 153 */     ZFolder mountpoint = mbox.getFolderByPath(mountpointPath);
/* 154 */     mbox.moveMessage(msg.getId(), mountpoint.getId());
/* 155 */     reportMsg = TestUtil.waitForMessage(hamMbox, "zimbra-spam-report ham");
/* 156 */     validateSpamReport(TestUtil.getContent(hamMbox, reportMsg.getId()), TestUtil.getAddress("user1"), "ham", "remote move", spamFolder.getPath(), inboxFolder.getPath(), TestUtil.getAddress("user4"));
/*     */     
/*     */ 
/* 159 */     hamMbox.deleteMessage(reportMsg.getId());
/*     */   }
/*     */   
/* 162 */   static Pattern PAT_REPORT_LINE = Pattern.compile("(.+): (.+)");
/*     */   
/*     */   private void spamReportEntryCheck(Map<String, String> report, String rprtKey, boolean lcase, String expected)
/*     */   {
/* 166 */     String actual = Strings.nullToEmpty((String)report.get(rprtKey));
/* 167 */     expected = Strings.nullToEmpty(expected);
/* 168 */     if (lcase)
/* 169 */       expected = expected.toLowerCase();
/* 170 */     assertEquals("Value for " + rprtKey, expected, Strings.nullToEmpty(actual));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void validateSpamReport(String content, String classifiedBy, String classifiedAs, String action, String sourceFolder, String destFolder, String destMailbox)
/*     */     throws IOException
/*     */   {
/* 178 */     BufferedReader reader = new BufferedReader(new StringReader(content));
/* 179 */     String line = null;
/* 180 */     Map<String, String> report = Maps.newHashMap();
/*     */     
/* 182 */     while ((line = reader.readLine()) != null) {
/* 183 */       Matcher m = PAT_REPORT_LINE.matcher(line);
/* 184 */       if (m.matches()) {
/* 185 */         report.put(m.group(1), m.group(2));
/*     */       }
/*     */     }
/* 188 */     reader.close();
/*     */     
/* 190 */     spamReportEntryCheck(report, "Classified-By", true, classifiedBy);
/* 191 */     spamReportEntryCheck(report, "Classified-As", false, classifiedAs);
/* 192 */     spamReportEntryCheck(report, "Action", false, action);
/* 193 */     spamReportEntryCheck(report, "Source-Folder", false, sourceFolder);
/* 194 */     spamReportEntryCheck(report, "Source-Folder", false, sourceFolder);
/* 195 */     spamReportEntryCheck(report, "Destination-Folder", false, destFolder);
/* 196 */     spamReportEntryCheck(report, "Destination-Mailbox", true, destMailbox);
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 202 */     Config config = Provisioning.getInstance().getConfig();
/* 203 */     config.setSpamHeaderValue(this.mOriginalSpamHeaderValue);
/* 204 */     config.setSpamIsSpamAccount(this.mOriginalSpamAccount);
/* 205 */     config.setSpamIsNotSpamAccount(this.mOriginalHamAccount);
/*     */     
/* 207 */     Account account = TestUtil.getAccount("user1");
/* 208 */     account.setMailSieveScript(this.mOriginalSieveScript);
/*     */     
/* 210 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 215 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/* 216 */     TestUtil.deleteTestData("user2", "zimbra-spam-report");
/* 217 */     TestUtil.deleteTestData("user3", "zimbra-spam-report");
/* 218 */     TestUtil.deleteTestData("user4", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 223 */     TestUtil.cliSetup();
/* 224 */     TestUtil.runTest(TestSpam.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSpam.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */